Passed
Pull Request — master (#88)
by Mathieu
03:54
created

MailerAdapter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 23
dl 0
loc 27
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A send 0 21 2
1
import { Injectable } from '@nestjs/common';
2
import { IMail } from 'src/Application/IMail';
3
import { IMailer } from 'src/Application/IMailer';
4
import { send, setApiKey } from '@sendgrid/mail';
5
import { ConfigService } from '@nestjs/config';
6
7
@Injectable()
8
export class MailerAdapter implements IMailer {
9
  constructor(
10
    private readonly configService: ConfigService
11
  ) {}
12
13
  public async send(mail: IMail): Promise<void> {
14
    const baseUrl = this.configService.get<string>('BASE_URL');
15
16
    try {
17
      setApiKey(this.configService.get<string>('SENDGRID_API_KEY'));
18
      await send({
19
        from: this.configService.get<string>('MAIL_SENDER'),
20
        templateId: mail.templateId,
21
        personalizations: [
22
          {
23
            to: mail.to,
24
            dynamicTemplateData: {
25
              ...mail.payload,
26
              baseUrl
27
            }
28
          }
29
        ],
30
      });
31
    } catch (error) {
32
      console.warn(error);
33
    }
34
  }
35
}
36